// pages/xqworks/xqworks.js
const app = getApp()
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    medal: app.data.imgurl + "medal.png",// 奖牌
    indexdd: 0,
    photo: app.data.imgurl + "works.png",
    lefttop: app.data.imgurl + "lefttop.png",
    worksd: [
      // { headsrc: app.data.imgurl +"headimg.png", type: "0", src: "../../video/2017123101.mp4", title: "来自炎炎夏日去大山里避暑的旅拍混剪", label: "#多媒体摄影", comment: "20", appreciate: "100", comments: [{ type: "0", fname: "胡杰", sname: "张飞", content: "消息不错" }] },
      {
        headsrc: app.data.imgurl + "headimg.png", type: "1", src: [app.data.imgurl + "swiper1.png", app.data.imgurl + "swiper1.png", app.data.imgurl + "swiper1.png"], title: "来自炎炎夏日去大山里避暑的旅拍混剪", phone: "华为O30", insprition: "我并非最专业的摄影师，只是由于兴趣爱好，才来参加这个活动，希望大家多多支持。", label: "#多媒图片", comment: "20", appreciate: "100", comments: [
          { headsrd: app.data.imgurl + "headimg.png", name: "sunda", comen: "图片拍的太棒了，作者大大怎么做到的呀？求赐教！", date: "20分钟前", replay: [{ fname: "sunda", replayconent: "期待你的碧玺" }, { fname: "山姆", replayconent: "后面会出的" }] },
          { headsrd: app.data.imgurl + "headimg.png", name: "rain", comen: "图片拍的太棒了，作者大大怎么做到的呀？求赐教！", date: "20分钟前", replay: [{ fname: "sunda", replayconent: "期待你的碧玺" }, { fname: "山姆", replayconent: "后面会出的" }] },

        ]
      }

    ],
    comment: app.data.imgurl + "comment.png", //评论图
    appreciate: app.data.imgurl + "appreciate.png", //赞图
    appreciates: app.data.imgurl + "hongxin.png",
    imgtypes: ["人像", "风光", "记录", "夜景", "微摄影", "商业", "微摄影", "微摄影"],
    frequnecy: app.data.imgurl + "worksnumbert.png",// 作品数量
    pinglun: "评论点什么",
    pinstate: "1",
    huifu: "回复",
    bisaizuopinginfourl: app.data.url + 'sheyingjia/Xzuoping1/zuopinginfo',
    dianzanbisaizuopin: app.data.url + 'sheyingjia/Xzuoping1/dianzan',
    bisaizuopinpinglunlist: app.data.url + 'sheyingjia/Xpage1/getzuopinlist',
    pinglunbisai: app.data.url + 'sheyingjia/Xzuoping1/pinglunzuopin',
    huifubisaipinglun: app.data.url + 'sheyingjia/Xzuoping1/huifuputong',
    shuju: '',
    create_time: 0,
    pinglunpage: 1,
    pinlun: [],
    xianzaipinlun: '',
    xiazaiurl: app.data.url + 'sheyingjia/Xxiazai1/sfxiazaiputong',
    dashangzuopin: app.data.url + 'sheyingjia/Xdashang1/dashangzuopin',


    hiddenmodalput:true,
    das:0,
  },

  //点击导航栏
  ssx: function (e) {
    console.log(e)
    var indexdd = e.currentTarget.dataset.index;
    this.setData({
      indexdd: indexdd
    })
  },
  //点击图片预览
  previewimg: function (e) {
    var src = this.data.shuju.tupian;
    wx.previewImage({
      current: 1,
      urls: src
    })
  },
  //点击人的姓名进行回复
  huifus: function (e) {
    var pinlunid = e.currentTarget.dataset.pinlunid;
    var userid = e.currentTarget.dataset.userid;
    var username = e.currentTarget.dataset.username;
    var pinlun = this.data.pinlun;
    var newpinlun = [];
    ea.each(pinlun, function (i, v) {
      if (v.id == pinlunid) {
        v.huifus = 1;
        v.tosname = username;
        v.tos = userid;
      }
      newpinlun.push(v);
    })
    this.setData({ pinlun: newpinlun })
  },
  //变更回复
  changehuifu: function (e) {
    var str = e.detail.value;
    var pinglunid = e.currentTarget.dataset.id;
    var pinlun = this.data.pinlun;
    var newpinlun = [];
    ea.each(pinlun, function (i, v) {
      if (v.id == pinglunid) {
        v.newhuifu = str;
        console.log(v);
      }
      newpinlun.push(v)
    })
    this.setData({ pinlun: newpinlun });
  },
  //回复
  kuaihuifu: function (e) {
    console.log(e);
    var pinglun = e.currentTarget.dataset.id;
    var tos = e.currentTarget.dataset.tos;
    var dir = e.currentTarget.dataset.str;
    var user = wx.getStorageSync('user');
    var that = this;
    if (!user) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return;
    }
    if (!dir) {
      wx.showToast({
        title: '请输入回复内容',
        icon: 'none'
      })
      return;
    }
    wx.showLoading({
      title: '回复中',
    })
    wx.request({
      url: that.data.huifubisaipinglun,
      data: { pinglun: pinglun, tos: tos, dir: dir, token: user.token },
      method: "POST",
      success: function (res) {
        wx.hideLoading();
        var data = res.data;
        console.log(data);
        if (data.code == 200) {
          var pinlun = that.data.pinlun;
          var newpinlun = [];
          ea.each(pinlun, function (i, v) {
            if (v.id == pinglun) {
              v.huifu = data.data;
              v.newhuifu = '';
              v.huifus = 0;
            }
            newpinlun.push(v);
          })
          that.setData({ pinlun: newpinlun });
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
      }
    })

  },
  //获取评论内容
  pinlunxianzai: function (e) {
    // console.log(e);
    var str = e.detail.value;
    this.setData({ xianzaipinlun: str });

  },
  //下载
  xiazai:function(){
    var shuju = this.data.shuju;
    var user = wx.getStorageSync('user');
    if(!user){
      wx.showToast({
        title: '请先登录',
        icon:'none'
      })
      return;
    }
    var that = this;
    wx.request({
      url: that.data.xiazaiurl,
      data: { id: shuju.id, token: user.token },
      method: "POST",
      success: function (res) {
        var data = res.data;
        console.log(data)
        if (data.code == 200) {
          if (shuju.leixin == '图片') {
            var tupian = shuju.tupian;
            console.log(tupian);
            ea.each(tupian, function (i, v) {
              wx.downloadFile({
                url: v, //仅为示例，并非真实的资源
                success(res) {
                  // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
                  // console.log(res);
                  if (res.statusCode === 200) {
                    var tempFilePath = res.tempFilePath;
                    console.log(tempFilePath);
                    wx.saveImageToPhotosAlbum({
                      filePath: tempFilePath,
                      success: function () {
                        wx.showToast({
                          title: '保存成功',
                        })
                      }
                    })
                  }
                }
              })
            })
          }else{
            
          }
          
        } else if (data.code == 205) {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
          setTimeout(function () {
            wx.navigateTo({
              url: 'pages/recharge/recharge',
            })
          }, app.data.times)
        } else if (data.code == 206) {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
          setTimeout(function () {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }, app.data.times)
        }
      }
    })
  },
  //评论
  fasong: function () {
    var str = this.data.xianzaipinlun;
    var that = this;
    var shuju = this.data.shuju;
    if (!str) {
      wx.showToast({
        title: '请输入你的评论内容',
        icon: 'none'
      })
      return;
    }
    var user = wx.getStorageSync('user');
    if (!user) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return;
    }
    wx.showLoading({
      title: '评论中',
    })
    wx.request({
      url: that.data.pinglunbisai,
      data: { str: str, token: user.token, id: shuju.id },
      method: "POST",
      success: function (res) {
        wx.hideLoading();
        var data = res.data;
        console.log(data);
        if (data.code == 200) {
          var pinlun = that.data.pinlun;
          var shuju = that.data.shuju;
          shuju.pinglun = data.data.count;
          pinlun = ea.addArray([data.data], pinlun);
          that.setData({ pinlun: pinlun, xianzaipinlun: '', shuju: shuju });
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    console.log(id);
    var tiaojian = { id: id };
    var user = wx.getStorageSync('user');
    var that = this;
    if (user) {
      tiaojian.token = user.token;
    }
    wx.showLoading({
      title: '加载中',
    })
    console.log(that.data.bisaizuopinginfourl);
    // return
    wx.request({
      url: that.data.bisaizuopinginfourl,
      data: tiaojian,
      method: "POST",
      success: function (res) {
        wx.hideLoading();
        var data = res.data;
        console.log(data);
        // return
        if (data.code == 200) {
          that.setData({ shuju: data.data });
          that.getpinlun();
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  //打赏
  qudashang:function(e){
    this.setData({ hiddenmodalput:false});
    
  },
  //取消打赏
  cancel:function(){
    this.setData({ hiddenmodalput: true });
  },
  //提交打赏
  confirm:function(){
    var das = this.data.das;
    var zuopingid = this.data.shuju.id;
    var user = wx.getStorageSync('user');
    var that = this;
    ea.request(that.data.dashangzuopin, { das: das, zuopingid: zuopingid,token:user.token},function(res){
      var data = res.data;
      console.log(data);
      if(data.code==206){
        wx.showToast({
          title: data.msg,
          icon:'none'
        })
        that.setData({ hiddenmodalput: true });
        setTimeout(function(){
          wx.navigateTo({
            url: '/pages/recharge/recharge',
          })
        },1500)
      }else if(data.code==200){
        wx.showToast({
          title:data.msg,
        })
        that.setData({ hiddenmodalput:true});
      }else{
        wx.showToast({
          title: data.msg,
          icon:'none'
        })
      }
    })
  },
  //填写打赏
  dashang:function(e){
    var dashang = parseFloat(e.detail.value);
    this.setData({ das: dashang});

  },
  //点赞作品
  dianzan: function (e) {
    var id = e.currentTarget.dataset.id;
    var tiaojian = { id: id };
    var user = wx.getStorageSync('user');
    if (user) {
      tiaojian.token = user.token;
    }
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.dianzanbisaizuopin,
      data: tiaojian,
      method: "POST",
      success: function (res) {
        wx.hideLoading();
        var data = res.data;
        if (data.code == 200) {
          var shuju = that.data.shuju;
          shuju.dianzan = data.data;
          shuju.wdz = 1;
          that.setData({ shuju: shuju });
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  //获取评论
  getpinlun: function () {
    var page = this.data.pinglunpage;
    var id = this.data.shuju.id;
    var create_time = this.data.create_time;
    // console.log(id);
    // return
    var that = this;
    if (page == 0) {
      wx.showToast({
        title: '没有更多了',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.bisaizuopinpinglunlist,
      data: { id: id, page: page, create_time: create_time },
      method: "POST",
      success: function (res) {
        wx.hideLoading();
        var data = res.data;
        console.log(data);
        if (data.code == 200) {
          if (data.data.data.length < 10) {
            that.setData({ pinglunpage: 0 })
          } else {
            that.setData({ pinglunpage: page + 1 });
          }
          var pinlun = that.data.pinlun;
          ea.each(data.data.data, function (i, v) {
            pinlun.push(v)
          })
          that.setData({ pinlun: pinlun, create_time: data.data.create_time });
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
      }, fail: function () {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getpinlun();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})